package cn.cdu.medical.mapper;

import cn.cdu.medical.entity.PrescriptionDetail;
import cn.cdu.medical.vo.PrescriptionDetailVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PrescriptionDetailMapper extends BaseMapper<PrescriptionDetail> {
    
    /**
     * 批量插入处方明细
     * @param details 处方明细列表
     * @return 影响行数
     */
    @Insert("<script>" +
            "INSERT INTO prescription_detail (" +
            "id, prescription_id, medicine_id, medicine_name, specification, " +
            "unit, amount, usage_method, frequency, days, " +
            "single_dose, total_dose, notes, create_time, update_time, " +
            "create_by, update_by, del_flag" +
            ") VALUES " +
            "<foreach collection='details' item='item' separator=','>" +
            "(" +
            "#{item.id}, #{item.prescriptionId}, #{item.medicineId}, #{item.medicineName}, #{item.specification}, " +
            "#{item.unit}, #{item.amount}, #{item.usageMethod}, #{item.frequency}, #{item.days}, " +
            "#{item.singleDose}, #{item.totalDose}, #{item.notes}, #{item.createTime}, #{item.updateTime}, " +
            "#{item.createBy}, #{item.updateBy}, #{item.delFlag}" +
            ")" +
            "</foreach>" +
            "</script>")
    int insertBatchDetail(@Param("details") List<PrescriptionDetail> details);
    
    /**
     * 根据处方ID查询明细列表
     * @param prescriptionId 处方ID
     * @return 明细列表
     */
    @Select("SELECT * FROM prescription_detail " +
            "WHERE prescription_id = #{prescriptionId} " +
            "AND del_flag = 0 " +
            "ORDER BY create_time ASC")
    @Results({
        @Result(id = true, column = "id", property = "id"),
        @Result(column = "prescription_id", property = "prescriptionId"),
        @Result(column = "medicine_id", property = "medicineId"),
        @Result(column = "medicine_name", property = "medicineName"),
        @Result(column = "specification", property = "specification"),
        @Result(column = "unit", property = "unit"),
        @Result(column = "amount", property = "amount"),
        @Result(column = "usage_method", property = "usageMethod"),
        @Result(column = "frequency", property = "frequency"),
        @Result(column = "days", property = "days"),
        @Result(column = "single_dose", property = "singleDose"),
        @Result(column = "total_dose", property = "totalDose"),
        @Result(column = "notes", property = "notes"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "update_time", property = "updateTime"),
        @Result(column = "create_by", property = "createBy"),
        @Result(column = "update_by", property = "updateBy"),
        @Result(column = "del_flag", property = "delFlag")
    })
    List<PrescriptionDetailVO> selectDetailsByPrescriptionId(@Param("prescriptionId") String prescriptionId);

    /**
     * 根据就诊记录ID查询处方明细
     * @param medicalRecordId 就诊记录ID
     * @return 处方明细列表
     */
    @Select("SELECT pd.* FROM prescription_detail pd " +
            "JOIN prescription p ON pd.prescription_id = p.id " +
            "WHERE p.medical_record_id = #{medicalRecordId} " +
            "ORDER BY p.create_time DESC")
    List<PrescriptionDetail> selectByMedicalRecordId(@Param("medicalRecordId") String medicalRecordId);
} 